package com.song.eduservice.controller;


import com.song.commonutils.ResultObj;
import com.song.eduservice.entity.EduChapter;
import com.song.eduservice.entity.chapter.ChapterVo;
import com.song.eduservice.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author song
 * @since 2020-03-29
 */
@Api(description = "课程大纲管理")
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {
    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 查询课程大纲列表
     *
     * @return
     */
    @ApiOperation(value = "按ID查询课程大纲列表")
    @GetMapping("/{course_id}")
    public ResultObj findAllChapter(@PathVariable String course_id) {
        List<ChapterVo> list = eduChapterService.findAllChapterById(course_id);
        return ResultObj.ok().data("data", list);
    }

    /**
     * 添加章节
     *
     * @param eduChapter
     * @return
     */
    @ApiOperation(value = "添加章节")
    @PostMapping("/")
    public ResultObj addChapter(@RequestBody EduChapter eduChapter) {
        boolean save = eduChapterService.save(eduChapter);
        if (save) {
            return ResultObj.ADD_SUCCESS;
        }
        return ResultObj.ADD_ERROR;
    }

    /**
     * 根据章节id查询章节
     *
     * @param chapterId
     * @return
     */
    @ApiOperation(value = "根据章节id查询章节")
    @GetMapping("/findChapterInfo/{chapterId}")
    public ResultObj findChapterInfo(@PathVariable String chapterId) {
        EduChapter eduChapter = eduChapterService.getById(chapterId);
        return ResultObj.ok().data("data", eduChapter);
    }

    /**
     * 修改章节
     *
     * @param eduChapter
     * @return
     */
    @ApiOperation(value = "修改章节")
    @PutMapping("/")
    public ResultObj updateChapter(@RequestBody EduChapter eduChapter) {
        boolean b = eduChapterService.updateById(eduChapter);
        if (b) {
            return ResultObj.UPDATE_SUCCESS;
        }
        return ResultObj.UPDATE_ERROR;
    }

    /**
     * 检查章节中是否有小节
     *
     * @param chapterId
     * @return
     */
    @ApiOperation(value = "检查章节中是否有小节")
    @GetMapping("/checkChapter/{chapterId}")
    public ResultObj checkChapter(@PathVariable String chapterId) {
        int b = eduChapterService.checkChapter(chapterId);
        return ResultObj.ok().data("data",b);
    }

    /**
     * 删除章节
     *
     * @param chapterId
     * @return
     */
    @ApiOperation(value = "删除章节")
    @DeleteMapping("/{chapterId}")
    public ResultObj deleteChapter(@PathVariable String chapterId) {
        if (eduChapterService.removeById(chapterId)) {
            return ResultObj.DELETE_SUCCESS;
        } else {
            return ResultObj.DELETE_ERROR;
        }
    }
}

